package com.sansui.blog.service.impl;

import com.sansui.blog.dao.TagDao;
import com.sansui.blog.pojo.Tag;
import com.sansui.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @name: TagServiceImpl
 * @author: Sansui
 * @Software: IDEA
 * @Blog: https://www.cnblogs.com/sansui6/
 * @date: 2022-03-28 22:54
 **/
@Service
public class TagServiceImpl implements TagService {


    @Autowired
    private TagDao tagDao;

    /**
     * 获取后台 所有标签信息
     * @return 所有标签信息
     * Integer page,Integer limit
     */

    @Override
    public Map<String, Object> getAllTagByPage(Integer page, Integer limit) {
        Map<String,Object> data = new HashMap<>(16);
        try {
            Integer pages = (page-1)*limit;
            List<Tag> tags = tagDao.findAllTag(pages,limit);
            Integer tagCount = tagDao.CountTag();
            data.put("code",0);
            data.put("msg","查询成功！");
            data.put("count",tagCount);
            data.put("data",tags);
        }catch (Exception e){
            data.put("code",400);
            data.put("msg","查询失败！");
        }

        return data;
    }

    @Override
    public List<Tag> findAll() {
        return tagDao.findAll();
    }

    /**
     * 删除标签信息
     * @return 是否删除成功
     */
    @Transactional
    @Override
    public Map<String, Object> deleteTag(Long tagId) {

        Integer deleteTagNum = tagDao.deleteTag(tagId);
        Map<String,Object> data = new HashMap<>(16);
        if(deleteTagNum != 0){
            data.put("code",200);
            data.put("msg","删除成功！");
        }else{
            data.put("code",400);
            data.put("msg","删除失败！");
        }
        return data;


    }

    /**
     * 批量删除标签信息
     * @return 批量删除标签信息
     */
    @Transactional
    @Override
    public Map<String, Object> deleteTagByIds(List<String> delList) {
        Map<String,Object> data = new HashMap<>(16);
        try{
            tagDao.deleteTagByIds(delList);
            data.put("code",200);
            data.put("msg","批量删除成功！");
        }catch (Exception e){
            data.put("code",400);
            data.put("msg","批量删除失败！");
        }
        return data;
    }



    /**
     * 根据name获取标签信息
     * @return 标签信息
     */
    @Override
    public Map<String, Object> tagSearchByName(String tagName) {
        Map<String,Object> data = new HashMap<>(16);
        List<Tag> tags = tagDao.tagSearchByName(tagName);
        Integer tagCount = tagDao.CountTagByLike(tagName);
        data.put("code",0);
        data.put("msg","查询成功！");
        data.put("count",tagCount);
        data.put("data",tags);
        return data;
    }


    /**
     * 保存标签信息
     * @return 保存标签信息
     */
    @Transactional
    @Override
    public Map<String, Object> addTag(String tagName,String username) {
        Map<String,Object> data = new HashMap<>(16);
        if (tagName == null) {
            data.put("code",500);
            data.put("msg","添加失败，标签数据不可为空！");
            return data;
        }

        Integer tagNum = tagDao.findTagByTagName(tagName);
        if(tagNum > 0){
            data.put("code",400);
            data.put("msg","添加失败，该标签已存在！");
            return data;
        }
        Tag tag = new Tag();
        tag.setTagName(tagName);
        tag.setCreateTime(new Date());
        tag.setUpdateTime(new Date());
        tag.setCreateUser(username);
        tag.setUpdateUser(username);

        Integer addNum = tagDao.addTag(tag);
        if(addNum != 1){
            data.put("code",400);
            data.put("msg","添加失败！");
            return data;
        }
        data.put("code",200);
        data.put("msg","添加成功！");
        return data;
    }


    /**
     * 根据tagId获取标签信息
     * @return 标签信息
     */
    @Override
    public Tag getTagById(Long tagId) {
        return tagDao.getTagById(tagId);
    }


    /**
     * 修改标签信息
     * @return 修改标签信息
     */
    @Transactional
    @Override
    public  Map<String, Object> updateTag(String tagName,Long tagId,String username){
        Map<String,Object> data = new HashMap<>(16);
        if (tagName == null) {
            data.put("code",500);
            data.put("msg","修改失败，标签数据不可为空！");
            return data;
        }

        Tag tag = getTagById(tagId);
        if(tag == null){
            data.put("code",500);
            data.put("msg","修改失败，标签不存在！");
            return data;
        }

        Integer tagNum = tagDao.findTagByTagName(tagName);
        if(tagNum > 0){
            data.put("code",400);
            data.put("msg","修改失败，该标签已存在！");
            return data;
        }

        tag.setTagName(tagName);
        tag.setUpdateTime(new Date());
        tag.setUpdateUser(username);

        Integer updateNum = tagDao.updateTag(tag);
        if(updateNum != 1){
            data.put("code",400);
            data.put("msg","修改失败！");
            return data;
        }

        data.put("code",200);
        data.put("msg","修改成功！");
        return data;

    }

    /**
     * 获取标签id、名称、以及对应博客数量
     * @return
     */
    @Override
    public List<Map<String, Object>> countTagInfo() {
        return tagDao.countTagInfo();
    }



}
